import request from "@/utils/request";
import { API_ENDPOINTS } from "@/config/api";

// 用户信息接口（与后端API响应保持一致）
export interface User {
  id: number;
  username: string;
  email: string;
  avatar_url?: string;
  email_verified_at?: string;
  created_at: string;
}

// 登录请求参数
export interface LoginParams {
  email: string;
  password: string;
}

// 注册请求参数
export interface RegisterParams {
  username: string;
  email: string;
  password: string;
  password_confirmation: string;
  agreeToTerms?: boolean;
}

// 登录响应数据
export interface LoginResponse {
  user: User;
  access_token: string;
  token_type: string;
}

// 认证相关API
export const authApi = {
  // 用户注册
  register(params: RegisterParams) {
    // 移除agreeToTerms字段，只发送后端需要的字段
    const { agreeToTerms, ...registerData } = params;
    return request.post<LoginResponse>(
      API_ENDPOINTS.AUTH.REGISTER,
      registerData
    );
  },

  // 用户登录
  login(params: LoginParams) {
    return request.post<LoginResponse>(API_ENDPOINTS.AUTH.LOGIN, params);
  },

  // 用户登出
  logout() {
    return request.post(API_ENDPOINTS.AUTH.LOGOUT);
  },

  // 获取当前用户信息
  getCurrentUser() {
    return request.get<User>(API_ENDPOINTS.AUTH.ME);
  },

  // 忘记密码
  forgotPassword(email: string) {
    return request.post(API_ENDPOINTS.AUTH.FORGOT_PASSWORD, { email });
  },

  // 重置密码
  resetPassword(params: {
    email: string;
    token: string;
    password: string;
    password_confirmation: string;
  }) {
    return request.post(API_ENDPOINTS.AUTH.RESET_PASSWORD, params);
  },

  // 邮箱验证
  verifyEmail(verificationCode: string) {
    return request.post(API_ENDPOINTS.AUTH.VERIFY_EMAIL, {
      verification_code: verificationCode,
    });
  },
};
